<?php
/*
$JA#COPYRIGHT$
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

class JElementFields extends JElement
{
	/*
	 * Element name
	 *
	 * @access	protected
	 * @var		string
	 */
	var	$_name = 'Fields'; 

	function fetchElement($name, $value, &$node, $control_name)
	{
		$temp = JRequest::get('request');
		$options = $this->getFields();	
		$object_name = $control_name.'['.$name.']';
		$result = '';
		$salary_checked = '';
		if (!is_array($value))
			$value = explode(',',$value);
		for($i=0; $i < count($options); $i++){
			if (is_array($value))
			{
				$check='';
				for ($j=0;$j<count($value);$j++){
					if ($value[$j]==$options[$i]->id){
						$check = 'checked="checked"';
					}
				}
			}
			else 
			{
				$temp_value = $value;
				if ($temp_value==$options[$i]->id)
					$check = 'checked="checked"';
				else 
					$check = '';
			}
			$result .= 	($i+1).'.<input type="checkbox" name="'.$object_name .'[]" value="'.$options[$i]->id.'" '.$check.' />';
			$result .= '<label>'.$options[$i]->label.'</label>';
			$result .= '<br />';
		}
		
		for ($j=0;$j<count($value);$j++){
			if($value[$j]=='salary') $salary_checked = 'checked="checked"';
		}
		
		$result .= 	'&nbsp;&nbsp;&nbsp;<input type="checkbox" name="'.$object_name .'[]" value="salary" '.$salary_checked.' />';
		$result .= '<label>'.JText::_('Salary').'</label>';
		$result .= '<br />';
		
		return $result;
	}
	
	function getFields()
	{
		$db =& JFactory::getDBO();
		
		$query = 'SELECT f.id,f.label'
		. ' FROM #__ja_form_fields AS f '
		. " WHERE f.published=1 and f.table_name = 'ja_jobs' and f.is_on_filter=1 and display!='hide_all' and (f.field_type='SELECT' or f.field_type='MSELECT') ORDER BY f.id"
		;
		$db->setQuery( $query );
		
		$options = $db->loadObjectList();		
		return $options;
	}
	
}
